<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/5/2
 * Time: 下午7:29
 */
namespace Common\Model;
use Think\Model;
class PageModel extends Model{
    protected $table;//无表名前缀的管理员表名
    protected $pageTable;//有前缀的管理员表
    protected $prefix;//表前缀

    /**构造函数初始化数据
     * @param string $adminTable
     * @param string $prefix
     */
    public function __construct($pageTable,$prefix=''){
        parent::__construct();
        $this -> prefix = empty($prefix) ? C('DB_PREFIX') : $prefix;
        $this -> table = $pageTable;
        $this -> menuTable = $this -> prefix.$pageTable;
    }

    /**用于返回值
     * @param $data
     * @param string $msg
     * @param int $status
     * @return array
     */
    public function returnInfo($data,$msg='',$status = 0){
            return (['data'=>$data,'msg'=>$msg,'status'=>$status]);
    }

    /**用于获取数据记录（读取列表时获取数据库全部记录，编辑时用于获取数据库单条记录）
     * @param $id
     * @param string $field
     * @param string $order
     * @return array
     */
    public function pagelist($id,$field='id',$order='id desc'){
        $list = [];
        $page = M($this -> table);
        $list = $page->field($field);
        if(!empty($id)){
            $list = $list -> where(['id'=>$id]);
        }
        $list = $list->order($order)->select();
        if(!$list){
            return $this->returnInfo('','读取数据失败！',1);
        }
        return $this -> returnInfo($list,'读取数据成功！',0);
    }

    /**用于修改状态
     * @param $id
     * @return array
     */
    public function ChangeStatus($id){
        $page = M($this -> table)->where(['id'=>$id])->find();
        if($page['show'] == 0){
            $list = M($this -> table)->where(['id'=>$id])->setField(['show'=>1]);
            if(!$list){
                $result =  $this -> returnInfo(0,'修改失败！',1);
            }
            $result = $this -> returnInfo(1,'在导航栏显示！',0);
        }else{
            $list = M($this->table)->where(['id'=>$id])->setField(['show'=>0]);
            if(!$list){
                $result = $this -> returnInfo(0,'修改失败！',1);
            }
            $result = $this -> returnInfo(0,'不在导航栏显示！',0);
        }
        return $result;
    }

    //  删除数据记录
    public function delList($id){
        $del = M($this -> table)->where(['id'=>$id])->delete();
        if(!$del){
            return $this -> returnInfo('','删除失败！',1);
        }
        return $this -> returnInfo('','删除成功！',0);
    }

    public function save(){

    }
}